[globalNotificationId].page.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { useEffect, useMemo } from 'react';
  2. import type {
  3. GetServerSideProps,
  4. GetServerSidePropsContext,
  5. NextPage,
  6. } from 'next';
  7. import dynamic from 'next/dynamic';
  8. import Head from 'next/head';
  9. import { useRouter } from 'next/router';
  10. import { objectIdUtils } from '@growi/core/dist/utils';
  11. import { useTranslation } from 'next-i18next';
  12. import type { Container } from 'unstated';
  13. import { Provider } from 'unstated';
  14. import type { CommonProps } from '~/pages/utils/commons';
  15. import { generateCustomTitle } from '~/pages/utils/commons';
  16. import { useCurrentUser } from '~/stores-universal/context';
  17. import { retrieveServerSideProps } from '../../../utils/admin-page-util';
  18. const AdminLayout = dynamic(() => import('~/components/Layout/AdminLayout'), {
  19. ssr: false,
  20. });
  21. const ManageGlobalNotification = dynamic(
  22. () =>
  23. import('~/client/components/Admin/Notification/ManageGlobalNotification'),
  24. { ssr: false },
  25. );
  26. const ForbiddenPage = dynamic(
  27. () =>
  28. import('~/client/components/Admin/ForbiddenPage').then(
  29. (mod) => mod.ForbiddenPage,
  30. ),
  31. { ssr: false },
  32. );
  33. const AdminGlobalNotificationNewPage: NextPage<CommonProps> = (props) => {
  34. const { t } = useTranslation('admin');
  35. useCurrentUser(props.currentUser ?? null);
  36. const router = useRouter();
  37. const { globalNotificationId } = router.query;
  38. const currentGlobalNotificationId = Array.isArray(globalNotificationId)
  39. ? globalNotificationId[0]
  40. : globalNotificationId;
  41. useEffect(() => {
  42. const toastError = import('~/client/util/toastr').then(
  43. (mod) => mod.toastError,
  44. );
  45. if (globalNotificationId == null) {
  46. router.push('/admin/notification');
  47. }
  48. if (
  49. currentGlobalNotificationId != null &&
  50. !objectIdUtils.isValidObjectId(currentGlobalNotificationId)
  51. ) {
  52. (async () => {
  53. (await toastError)(
  54. t('notification_settings.not_found_global_notification_triggerid'),
  55. );
  56. router.push('/admin/global-notification/new');
  57. })();
  58. return;
  59. }
  60. }, [currentGlobalNotificationId, globalNotificationId, router, t]);
  61. const title = t('external_notification.external_notification');
  62. const customTitle = generateCustomTitle(props, title);
  63. const injectableContainers: Container<any>[] = useMemo(() => [], []);
  64. useEffect(() => {
  65. (async () => {
  66. const AdminNotificationContainer = (
  67. await import('~/client/services/AdminNotificationContainer')
  68. ).default;
  69. const adminNotificationContainer = new AdminNotificationContainer();
  70. injectableContainers.push(adminNotificationContainer);
  71. })();
  72. }, [injectableContainers]);
  73. if (props.isAccessDeniedForNonAdminUser) {
  74. <ForbiddenPage />;
  75. }
  76. return (
  77. <Provider inject={[...injectableContainers]}>
  78. <AdminLayout componentTitle={title}>
  79. <Head>
  80. <title>{customTitle}</title>
  81. </Head>
  82. {currentGlobalNotificationId != null && router.isReady && (
  83. <ManageGlobalNotification
  84. globalNotificationId={currentGlobalNotificationId}
  85. />
  86. )}
  87. </AdminLayout>
  88. </Provider>
  89. );
  90. };
  91. export const getServerSideProps: GetServerSideProps = async (
  92. context: GetServerSidePropsContext,
  93. ) => {
  94. const props = await retrieveServerSideProps(context);
  95. return props;
  96. };
  97. export default AdminGlobalNotificationNewPage;